//解析url
function getQueryStringArgs(){
  var qs = (location.search.length>0?location.search.substring(1):"");
  var args = {};
  var items = qs.length?qs.split("&"):[];
  var item = null;
  var name = null;
  var value = null;
  len = items.length;
  for (var i = 0; i < len; i++) {
    item = items[i].split("=");
    name = decodeURIComponent(item[0]);
    value = decodeURIComponent(item[1]);
    if (name.length) {
      args[name] = value;
    }
  }
  return args;
}

//发表文章
function publish(articleContent,articleOutline,articleTag,createtime,title,type){
	var url;
	if(type===0){
		url="../publish_action.jsp";
	}else if(type===1){
		url="../publish_action.jsp?id="+sessionStorage.id;
	}
	$.post(url, {  
			html : articleContent,
			outline : articleOutline,
			tag : articleTag,
			username : sessionStorage.username,
			title:title,
			createtime:createtime
		}, 
		function(data) {
			if(data=="false"){
				alert("发表失败！请重新尝试！");
			}else{
				alert(data);
				editor.clear();
				setContent(" "," "," ");	
			}
	});
}
$(document).ready(function() {
	setContent(sessionStorage.title,sessionStorage.main,sessionStorage.tag);
	$("#send").click(function(){
		if(sessionStorage.username=="undefined"){
			alert("您还未登录账号，请先登录！+1");
		}
		// 检查文章内容是否为空
		var has = true;
		var cont = getContentTxt().trim();
		if(cont=="")has= false;
		if(has){
			var title = $("#articleTitle").val().trim();
			// 检查文章标题是否为空
			if(title==""){alert("文章标题不允许为空！");}
			else{
				var articleContent = getContent();//文章内容
				var articleOutline = getContentTxt();//文章概要
				var articleTag = $("#articleTip").val();//文章标签
				var articleAuthor = sessionStorage.username;//文章作者
				var createtime = new Date().toLocaleString();//发表时间
				if(sessionStorage.id==" " || sessionStorage.id==undefined || sessionStorage.id=="undefined"){
					//发表
					publish(articleContent,articleOutline,articleTag,createtime,title,0);
				}
				else{
					//修改
					publish(articleContent,articleOutline,articleTag,createtime,title,1);
				}
				
				
			}
		}
	});
});